﻿#include "page.h"
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "mainPage.h"
#include "mp3Page.h"
#include "music.h"

static unsigned char playFlag = 0;
static lv_obj_t *musicName;
static lv_obj_t *playLabel;
static lv_obj_t *volLabel;
static lv_obj_t *btnVolUp;
static lv_obj_t *btnVolDown;
static lv_obj_t *btnPlay;
static lv_obj_t *btnNext;
static lv_obj_t *btnPrev;
static lv_style_t style;
static char volStr[4] = {0};
void play_event(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t *obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        int32_t value = lv_bar_get_value(obj);
        if (key == LV_KEY_ENTER)
        {
            if (playFlag)
            {
                music_stop();
                playFlag = 0;
                lv_label_set_text(playLabel, LV_SYMBOL_PLAY);
            }
            else
            {
                music_play();
                playFlag = 1;
                lv_label_set_text(playLabel, LV_SYMBOL_STOP);
            }
        }
    }
}

void next_event(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t *obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        int32_t value = lv_bar_get_value(obj);
        if (key == LV_KEY_ENTER)
        {
            music_next();
            lv_label_set_text_fmt(musicName, "%s", music_name_now());
        }
    }
}

void last_event(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t *obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        int32_t value = lv_bar_get_value(obj);
        if (key == LV_KEY_ENTER)
        {
            music_last();
            lv_label_set_text_fmt(musicName, "%s", music_name_now());
        }
    }
}

void vol_up_event(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t *obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        int32_t value = lv_bar_get_value(obj);
        if (key == LV_KEY_ENTER)
        {
            music_set_vol(music_get_vol() + 5);
            sprintf(volStr, "%d", music_get_vol());
            lv_label_set_text_fmt(volLabel, volStr);
        }
    }
}
void vol_down_event(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t *obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        int32_t value = lv_bar_get_value(obj);
        if (key == LV_KEY_ENTER)
        {
            music_set_vol(music_get_vol() - 5);
            sprintf(volStr, "%d", music_get_vol());
            lv_label_set_text_fmt(volLabel, volStr);
        }
    }
}


void show_mp3_page()
{
    lv_group_remove_all_objs(group);
    lv_group_add_obj(group, btnPlay);
    lv_group_add_obj(group, btnNext);
    lv_group_add_obj(group, btnVolDown);
    lv_group_add_obj(group, btnVolUp);
    lv_group_add_obj(group, btnPrev);

    lv_scr_load(mp3Page.page);
    push_page(&mp3Page);
}

lv_obj_t *mp3_btn_create(int x, int y)
{
    lv_obj_t *btn = lv_btn_create(mp3Page.page);
    lv_obj_set_pos(btn, x, y);
    lv_obj_add_style(btn, &style, 0);
    lv_obj_set_width(btn, 60);


    return btn;
}
lv_obj_t *mp3_btn_add_labe(lv_obj_t *btn, char *icon)
{
    lv_obj_t *label = lv_label_create(btn);
    lv_label_set_text(label, icon);
    lv_obj_center(label);
    return label;
}
void set_music_name(char *name)
{
    lv_label_set_text(musicName, name);
}
void page_mp3_init()
{
    mp3Page.page = lv_obj_create(0);

    //按钮样式
    lv_style_init(&style);
    lv_style_set_bg_opa(&style, 0);
    lv_style_set_border_opa(&style, 255);
    lv_style_set_border_side(&style, LV_BORDER_SIDE_FULL);
    lv_style_set_border_color(&style, lv_color_hex(0xffffff));
    lv_style_set_border_width(&style, 2);
    musicName = lv_label_create(mp3Page.page);
    lv_label_set_long_mode(musicName, LV_LABEL_LONG_SCROLL_CIRCULAR);     /*Circular scroll*/
    lv_obj_set_width(musicName, 200);
    lv_label_set_text_fmt(musicName, "%s", music_name_now());
    lv_obj_set_pos(musicName, 60, 30);
    volLabel = lv_label_create(mp3Page.page);
    lv_obj_set_pos(volLabel, 155, 190);
    
    lv_label_set_text_fmt(volLabel, "60");
    /*Create an image button*/
    btnVolDown = mp3_btn_create(70, 180);
    btnVolUp = mp3_btn_create(190, 180);
    btnPlay = mp3_btn_create(130, 110);
    btnNext = mp3_btn_create(210, 110);
    btnPrev = mp3_btn_create(50, 110);

    mp3_btn_add_labe(btnVolDown, LV_SYMBOL_VOLUME_MID);
    mp3_btn_add_labe(btnVolUp, LV_SYMBOL_VOLUME_MAX);
    playLabel = mp3_btn_add_labe(btnPlay, LV_SYMBOL_PLAY);
    mp3_btn_add_labe(btnNext, LV_SYMBOL_NEXT);
    mp3_btn_add_labe(btnPrev, LV_SYMBOL_PREV);

    lv_obj_add_event_cb(btnVolDown, vol_down_event, LV_EVENT_KEY, NULL);
    lv_obj_add_event_cb(btnVolUp, vol_up_event, LV_EVENT_KEY, NULL);
    lv_obj_add_event_cb(btnPlay, play_event, LV_EVENT_KEY, NULL);
    lv_obj_add_event_cb(btnNext, next_event, LV_EVENT_KEY, NULL);
    lv_obj_add_event_cb(btnPrev, last_event, LV_EVENT_KEY, NULL);

    mp3Page.show = show_mp3_page;
}
